package com.mcxx.modules.base.service;



import com.alibaba.fastjson.JSONObject;
import com.yinhai.yhdcs.DcsClient;
import com.yinhai.yhdcs.client.cloudmodel.*;
import com.yinhai.yhdcs.core.common.OSResult;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;


@Service("fileUpDownService")
public class FileUpDownService {

    String yhdcsUrl = "http://10.72.0.52:10010/yhdcs";

    String accessKeyId = "cG9ydGFsMTYwMjc1NDA3ODg1OA==";

    String accessKeySecret = "_Xq1jsp04LABRp6U2Y9rk83OPIhj5r86g8RM_DV0";

    String bucketId = "zhihuiminzheng";

    String customProperty = "customProperty";

    String customPropertyValue = "customPropertyValue";


    public OSResult uploadFile(File file) throws IOException {
        return uploadFile(null, file);
    }


    public OSResult uploadFile(String objectName, InputStream inputStream) throws IOException {
        return uploadFile(objectName, inputStream);
    }


    private OSResult uploadFile(String objectName, Object fileObj) throws IOException {

        DcsClient client = new DcsClient(accessKeyId, accessKeySecret);
        client.setYhdcsurl(yhdcsUrl);


        PutObjectRequest putObjectRequest = null;
        if(fileObj instanceof InputStream) {
            putObjectRequest = new PutObjectRequest(bucketId, (InputStream)fileObj, objectName);
        }
        else {
            putObjectRequest = new PutObjectRequest(bucketId, (File)fileObj);
        }

        ObjectMetadata objectMetadata = new ObjectMetadata();

        objectMetadata.setObjectAcl(AccessControlList.PublicReadWrite);

        objectMetadata.setObjectStoreType(StoreType.Standard);

        objectMetadata.addUserMetadata(customProperty, customPropertyValue);
        putObjectRequest.setObjectMetadata(objectMetadata);

        OSResult result = client.putObject(putObjectRequest);
        return result;
    }


    public void testDownloadInputStrem() throws IOException {

        String yhdcsUrl = "http://10.72.0.52:10010/yhdcs";

        String accessKeyId = "cG9ydGFsMTYwMjc1NDA3ODg1OA==";

        String accessKeySecret = "_Xq1jsp04LABRp6U2Y9rk83OPIhj5r86g8RM_DV0";

        String bucketId = "zhihuiminzheng";

        String objectId = "5ee68c75aa24439aae5780a39d85a97b";

        DcsClient client = new DcsClient(accessKeyId, accessKeySecret);
        client.setYhdcsurl(yhdcsUrl);
        GetObjectRequest getObjectRequest = new GetObjectRequest(bucketId, objectId, null);
        OSObject result = client.getObject(getObjectRequest);
        FileOutputStream fos = new FileOutputStream("C:\\Users\\75140\\Desktop\\yhcloud1.txt");
        byte[] b = new byte[1024];
        int len = 0;
        InputStream is = result.getContent();
        while((len = is.read(b)) != -1){
            fos.write(b,0,len);
        }
        is.close();
        fos.close();
    }


    public void testDownloadFile() throws IOException {

        String yhdcsUrl = "http://10.72.0.52:10010/yhdcs";

        String accessKeyId = "cG9ydGFsMTYwMjc1NDA3ODg1OA==";

        String accessKeySecret = "_Xq1jsp04LABRp6U2Y9rk83OPIhj5r86g8RM_DV0";

        String bucketId = "zhihuiminzheng";

        String objectId = "5ee68c75aa24439aae5780a39d85a97b";

        DcsClient client = new DcsClient(accessKeyId, accessKeySecret);
        client.setYhdcsurl(yhdcsUrl);
        GetObjectRequest getObjectRequest = new GetObjectRequest(bucketId, objectId, null);
        ObjectMetadata result = client.getObject(getObjectRequest, new File("C:\\Users\\75140\\Desktop\\yhcloud2.txt"));
    }
}
